/*
    Copyright 2006 Andrew Wilkinson <andrew@indiegigs.co.uk>

    This file is part of GL2x.

    GL2x is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    GL2x is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GL2x; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#include <sys/time.h>

#include "gl.h"
#include "gp2x.h"

// Left/Right Bottom/Top Back/Front
#define LBB 0
#define LTB 1
#define RBB 2
#define RTB 3
#define LBF 4
#define LTF 5
#define RBF 6
#define RTF 7

float coords[][3] = {{-1.0, -1.0, -1.0},
                     {-1.0,  1.0, -1.0},
                     { 1.0, -1.0, -1.0},
                     { 1.0,  1.0, -1.0},
                     {-1.0, -1.0,  1.0},
                     {-1.0,  1.0,  1.0},
                     { 1.0, -1.0,  1.0},
                     { 1.0,  1.0,  1.0}
                    };

float colours[][3] = {{0.5, 0.5, 0.5},
                      {1.0, 0.0, 0.0},
                      {0.0, 1.0, 0.0},
                      {1.0, 1.0, 0.0},
                      {0.0, 0.0, 1.0},
                      {1.0, 0.0, 1.0},
                      {0.0, 1.0, 1.0},
                      {1.0, 1.0, 1.0}
                     };

int sides[][4] = {{LBB, LTB, RBB, RTB},
                  {LBF, LTF, RBF, RTF},
                  {LBB, LTB, LBF, LTF},
                  {RBB, RTB, RBF, RTF},
                  {LBB, LBF, RBB, RBF},
                  {LTB, LTF, RTB, RTF}};

int main(int argc, char* argv[]) {
    int side, v, frame, coord;
    struct timeval start;
    struct timeval now;

    gettimeofday(&start, NULL);

    GLenum shade = GL_SMOOTH;

    frame = 0;

    glInit();

    glEnable(GL_DEPTH_TEST);

    glShadeModel(shade);

    while(!GP2x_input_pressed(START)) {

    if(GP2x_input_pressed(BUTTON_A)) {
        if(shade == GL_SMOOTH) {
            shade = GL_FLAT;
        } else {
            shade = GL_SMOOTH;
        }
        glShadeModel(shade);
    }

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
    gettimeofday(&now, NULL);
    glRotatef(((now.tv_sec - start.tv_sec)*1000000 + (now.tv_usec - start.tv_usec)) / 50000, 0.5, 0.5, 0.0);

    for(side=0; side<6; side++) {
        glBegin(GL_TRIANGLE_STRIP);

        for(v=0; v<4; v++) {
            coord = sides[side][v];
            glColor3f(colours[coord][0], colours[coord][1], colours[coord][2]);
            glVertex3f(coords[coord][0]*0.25, coords[coord][1]*0.25, coords[coord][2]*0.25+0.5);
        }

        glEnd();
    }

    glPopMatrix();

    GP2x_flip();

    frame++;
    }

    glFinalize();

    return 0;
}
